package logic

import (
	"context"
	"fmt"
	"jingdong-product/models/mysql"

	"jingdong-product/productrpc/internal/svc"
	"jingdong-product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// register
func (l *RegisterLogic) Register(in *productrpc.RegisterRequest) (*productrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	err := mysql.DB.Debug().Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &productrpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
